﻿using BrnMall.Core;
using BrnMall.Services;
using QIDong.WeApp.Models;
using System;
using System.Collections.Generic;
using WeiTap.Restful;
using BrnMall.Core.Domain.Rader;
using BrnMall.Manager.Manager.Radar;
using BrnMall.Manager.Domain.Rander;
using BrnMall.Manager.Manager.AddressBook;
using BrnMall.Services.WJK;
using BrnMall.Manager.Domain.AddressBook;

namespace BrnMall.Web.RestApi.Controllers
{
    /// <summary>
    /// 雷达数据控制器类
    /// </summary>
    public class RadarController : WebApi
    {

        /// <summary>
        /// 行为栏
        /// </summary>
        /// <param name="Uid">用户名</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        /// <returns></returns>
        public List<RadarTimeModel> BehaviorList(int staffId, string startTime = "", string endTime = "")
        {
            var modelList = ActionRecordManager.GetStaffIdByActionType(0, staffId, startTime, endTime);
            List<RadarTimeModel> rtmList = new List<RadarTimeModel>();
            foreach (RadarNumInfo rni in modelList)
            {
                rtmList.Add(new RadarTimeModel
                {
                    actionType = rni.actionType,
                    num = rni.Total
                });
            }
            return rtmList;
        }

        /// <summary>
        /// 人栏
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="pageSize"></param>
        /// <param name="page"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        public List<RadarTimeModel> TimeList(int staffId, int page = 1, int pageSize = 10, string startTime = "", string endTime = "")
        {
            var rtmList = new List<RadarTimeModel>();
            var modelList = ActionRecordManager.GetGroupTotalByUid(staffId, startTime, endTime, page, pageSize);
            foreach (var item in modelList)
            {
                var user = Users.GetPartUserById(item.Uid);
                rtmList.Add(new RadarTimeModel
                {
                    staffId = staffId,
                    uid = user.Uid,
                    nickname = user != null ? user.NickName : string.Empty,
                    avatar = user != null ? user.Avatar : string.Empty,
                    num = item.Total
                });
            }
            return rtmList;
        }

        /// <summary>
        /// 时间详细信息
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="staffId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<RadarTimeModel> TimeDetailList(int Uid, int staffId, string startTime = "", string endTime = "")
        {
            var rtmList = new List<RadarTimeModel>();
            var modelList = ActionRecordManager.GetStaffIdByActionType(Uid, staffId, startTime, endTime);
            foreach (RadarNumInfo rni in modelList)
            {
                UserInfo ui = AdminUsers.GetUserById(rni.StaffId);
                rtmList.Add(new RadarTimeModel
                {
                    nickname = ui != null ? ui.NickName : string.Empty,
                    actionType = rni.actionType,
                    num = rni.Total
                });
            }
            return rtmList;
        }

        /// <summary>
        /// 第一栏时间列表
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public object RadarTimeList(int staffId, int page = 1, int pageSize = 10)
        {
            
            var rtmLists = new List<RadarTimeInfoModel>();
            List<ActionRecord> modelList = ActionRecordManager.GetRadarTimeList(staffId, page, pageSize);
            foreach (var rni in modelList)
            {
                Clients cs = ClientManager.GetByUid(rni.Uid, staffId);
                RadarTimeInfoModel rtim = rtmLists.Find(o => o.addtime.ToString("yyyy/MM/dd HH:mm") == rni.Addtime.ToString("yyyy/MM/dd HH:mm"));
                if (rtim == null)
                {
                    var rtmList = new List<RadarTimeModel>();
                    rtmList.Add(new RadarTimeModel
                    {
                        uid = rni.Uid,
                        staffId =rni.StaffId,
                        nickname = cs != null ? cs.MemoName : string.Empty,
                        avatar = cs != null ? cs.Avatar : string.Empty,
                        actionType = rni.ActionType
                    });
                    rtmLists.Add(new RadarTimeInfoModel
                    {
                        addtime = rni.Addtime,
                        rtmList = rtmList
                    });
                }
                else
                    rtim.rtmList.Add(new RadarTimeModel
                    {
                        uid = rni.Uid,
                        staffId = rni.StaffId,
                        nickname = cs != null ? cs.MemoName : string.Empty,
                        avatar = cs != null ? cs.Avatar : string.Empty,
                        actionType = rni.ActionType
                    });
            }
            return rtmLists;       
        }

        /// <summary>
        /// 互动消息列表
        /// </summary>
        /// <param name="clientid"></param>
        /// <param name="staffId"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public object ActionRecordList(int staffId, int clientid = 0, int page = 1, int pageSize = 15)
        {
            var client = ClientManager.Get(clientid);
            if (client == null)
                throw new Exception("客户不存在");

            var user = Users.GetPartUserById(client.Uid);

            var actionRecordlist = ActionRecordManager.GetList(client.Uid, staffId, page, pageSize);

            var nickName = string.IsNullOrEmpty(client.MemoName) ? user.NickName : client.MemoName;
            var list = new List<object>();
            foreach (var item in actionRecordlist.Items)
            {
                var info = new
                {
                    Title = string.Format("{0} {1}", nickName, ActionRecords.GetActionTitle(item.ActionType)),
                    AddTime = item.Addtime.ToString("yyyy-MM-dd")
                };
                list.Add(info);
            }

            var pageModel = new PageModel(pageSize, page, Convert.ToInt32(actionRecordlist.TotalItems));
            var model = new
            {
                NickName = client.MemoName,
                Avater = user.Avatar,
                List = list,
                PageModel = pageModel
            };
            return model;
        }

        /// <summary>
        /// 时间详情
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="actionType"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public object BehaviorDetail(int staffId,int actionType, int page = 1, int pageSize = 10) {
            var rtmLists = new List<RadarTimeInfoModel>();
            List<ActionRecord> modelList = ActionRecordManager.GetRadarTimeDetail(staffId, actionType, page, pageSize);
            foreach (var rni in modelList)
            {
                Clients cs = ClientManager.GetByUid(rni.Uid, staffId);
                RadarTimeInfoModel rtim = rtmLists.Find(o => o.addtime.ToString("yyyy/MM/dd HH:mm") == rni.Addtime.ToString("yyyy/MM/dd HH:mm"));
                if (rtim == null)
                {
                    var rtmList = new List<RadarTimeModel>();
                    rtmList.Add(new RadarTimeModel
                    {
                        uid = rni.Uid,
                        staffId = rni.StaffId,
                        nickname = cs != null ? cs.MemoName : string.Empty,
                        avatar = cs != null ? cs.Avatar : string.Empty,
                        actionType = rni.ActionType
                    });
                    rtmLists.Add(new RadarTimeInfoModel
                    {
                        addtime = rni.Addtime,
                        rtmList = rtmList
                    });
                }
                else
                    rtim.rtmList.Add(new RadarTimeModel
                    {
                        uid = rni.Uid,
                        staffId = rni.StaffId,
                        nickname = cs != null ? cs.MemoName : string.Empty,
                        avatar = cs != null ? cs.Avatar : string.Empty,
                        actionType = rni.ActionType
                    });
            }
            return rtmLists;
        }
    }
}
